<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
      function queryData(url) {
        const p = new Promise(function(resolve, reject) {
          const xhr = new XMLHttpRequest()
          xhr.open('get', url)
          xhr.send()

          xhr.onreadystatechange = function() {
            if( xhr.readyState !== 4 ) { return }
            if( xhr.readyState === 4 && xhr.status === 200 ) {
              resolve(xhr.responseText)
            } else {
              reject('服务器有问题')
            }
          }
        })
        return p
      }

      queryData('http://127.0.0.1:3000/data')
        .then(function(res) {
          console.log(res)
        })
        .catch(function(err) {  // 捕获异常的
          console.log('捕获到异常了', err)
        })
        // finally : 不管之前是否成功，我都会最后执行一次 finally
        .finally(function() {
          console.log('都是他舅')
        })
  </script>
</body>
</html>